# controlls the UI for users to chang their first and last names

class PeopleController < ApplicationController
  layout 'admin_standard'
  before_filter :authenticate
  active_scaffold :person
  active_scaffold :person do |config|
        config.columns = [:first_name, :last_name]
        config.actions.exclude :update, :create
   end

  # GET /people
  # GET /people.xml
  def index
    @people = Person.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @people }
    end
  end

  ## 
  # Display the current user
  def show
    @person = Person.find_by_username(params[:username])

    if @person.nil?
        @person = Person.new
        @person.username = params[:username]
        @person.save
    end
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @person }
    end
  end

  ##
  # Allows the user to view the edit screen for his or her name.
  # The currently logged in user can only get to their own edit page. 
  def edit
    @person = Person.find(params[:id])
    
    if @person.username != session[:username]
      flash[:notice] = "Stop messing with other people's names. It's rude."
      redirect_to( :controller => :people, :username => @person.username, :action => :show )
    end
                
  end

  

  ##
  # Allows the user to change his or her name.
  # The currently logged in user can only edit their own name. 
  def update
    @person = Person.find(params[:id])

   if @person.username != session[:username]
        flash[:notice] = "Stop messing with other people's names. It's rude."
        redirect_to( :controller => :people, :username => @person.username, :action => :show )
   else
      respond_to do |format|
        if @person.update_attributes(params[:person])
          flash[:notice] = 'Your name was successfully updated.'
          format.html { redirect_to( :controller => :people, :username => @person.username, :action => :show ) }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @person.errors, :status => :unprocessable_entity }
        end
      end
    end
  end


end
